package com.example.demo.leetcode.thread;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程异常处理
 *
 */
public class ChildThread implements Runnable{

        static ChildThreadExceptionHandler handler;

        static{
            handler = new ChildThreadExceptionHandler();
            Thread.setDefaultUncaughtExceptionHandler(handler);
        }

        @Override
        public void run() {
            System.out.println("开始运行");
            exceptionMethod();
            System.out.println("结束运行");

        }

        public void exceptionMethod(){
            throw new RuntimeException("运行时异常");
        }

    static class ChildThreadExceptionHandler implements Thread.UncaughtExceptionHandler{

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("线程:"+t.getName()+"出现异常，异常信息:"+e.getMessage());
        }
    }


    public static void main(String[] args) {
            Thread thread = new Thread(new ChildThread());
            Thread thread2 = new Thread(new ChildThread());
            ThreadPoolExecutor pool = new ThreadPoolExecutor(10,10,10, TimeUnit.SECONDS,new ArrayBlockingQueue(1000));
            pool.execute(thread);
            pool.execute(thread2);
    }


}
